cmake_minimum_required(VERSION 3.15)
project(GuessColorGame)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置Unicode支持
add_definitions(-DUNICODE -D_UNICODE)

# 设置编译器选项
if(MSVC)
    # MSVC编译器选项
    add_compile_options(/W4 /WX)
else()
    # GCC/Clang编译器选项
    add_compile_options(-Wall -Wextra -Werror)
endif()

# 设置Windows特定选项
if(WIN32)
    # Windows API库
    set(WIN32_LIBS user32 gdi32 comctl32)
    
    # 对于MinGW/GCC编译器，使用不同的链接器选项
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
        # MinGW/GCC链接器选项 - 禁用控制台窗口
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -mwindows")
    else()
        # MSVC链接器选项 - 禁用控制台窗口
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")
    endif()
endif()

# 包含目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/Header_Files
    ${CMAKE_CURRENT_SOURCE_DIR}/Library_Files
    ${CMAKE_CURRENT_SOURCE_DIR}/Source_Files
)

# 自动查找源文件
file(GLOB SOURCES "Source_Files/*.cpp")

# 头文件列表（用于依赖跟踪）
set(HEADERS
    Header_Files/MainWindow.hpp
    Header_Files/GameWindow.hpp
    Header_Files/Color.hpp
    Header_Files/YunUtility.hpp
)

# 打印找到的源文件用于调试
message(STATUS "Found source files:")
foreach(source ${SOURCES})
    message(STATUS "  ${source}")
endforeach()

# 创建可执行文件（Windows GUI应用程序）
add_executable(${PROJECT_NAME} WIN32 ${SOURCES} ${HEADERS})

# 链接库
target_link_libraries(${PROJECT_NAME} 
    PRIVATE 
    ${WIN32_LIBS}
)

# 设置输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Output_Files
)

# 设置构建目录（可选，CMake会自动管理）
# set(CMAKE_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Build_Files)

# 复制资源文件到输出目录
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/Resource_Files/favicon.ico
    # $<TARGET_FILE_DIR:${PROJECT_NAME}>
    ${CMAKE_CURRENT_SOURCE_DIR}/Output_Files/icos/favicon.ico
    COMMENT "Copying resource files to output directory"
)

# 安装目标
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

# 安装资源文件
install(FILES Resource_Files/favicon.ico
    DESTINATION bin
)

# 可选：生成编译数据库（用于IDE支持）
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 可选：设置调试和发布配置
if(CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configurations" FORCE)
endif()

if(MSVC)
    # MSVC特定设置
    target_compile_options(${PROJECT_NAME} PRIVATE
        $<$<CONFIG:Debug>:/Od /Zi /MDd>
        $<$<CONFIG:Release>:/O2 /MD>
    )
else()
    # GCC/Clang特定设置
    target_compile_options(${PROJECT_NAME} PRIVATE
        $<$<CONFIG:Debug>:-g -O0 -fno-omit-frame-pointer>
        $<$<CONFIG:Release>:-O3>
    )
endif()

# 可选：添加测试目标（如果将来需要）
# enable_testing()
# add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME})

# 可选：添加代码格式检查（如果安装了clang-format）
# find_program(CLANG_FORMAT clang-format)
# if(CLANG_FORMAT)
#     add_custom_target(format
#         COMMAND ${CLANG_FORMAT} -i ${SOURCES} ${HEADERS}
#         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
#         COMMENT "Formatting source files"
#     )
# endif()